package gold.digger;

import gold.utils.UF;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC990 {

    /*
     * @param 此题目直接用别人代码，自己只理解思想
     * @return:
     */
    public boolean equationsPossible(String[] equations) {
        // 26 个英文字母
        UF uf = new UF(26);
        // 先让相等的字母形成连通分量
        for (String eq : equations) {
            if (eq.charAt(1) == '=') {
                char x = eq.charAt(0);
                char y = eq.charAt(3);
                uf.union(x - 'a', y - 'a');
            }
        }

        // 先让相等的字母形成连通分量
        for (String eq : equations) {
            if (eq.charAt(1) == '!') {
                char x = eq.charAt(0);
                char y = eq.charAt(3);
                if (uf.connected(x - 'a', y - 'a')) {
                    return false;
                }
            }
        }

        return true;
    }


    public static void main(String[] args) {
        LC990 an = new LC990();

        String[] arr = {"a==b", "b!=a"};
        System.out.println(an.equationsPossible(arr));
    }
}
